﻿namespace Barrage.Controls
{
    public class LogListView : ListViewEx
    {
        private readonly ContextMenuStrip rightClickMenu;

        private ColumnHeader headerMsg;

        public int IdWidth { get; set; } = 80;


        public int TimeWidth { get; set; } = 240;


        public int LogTypeWidth { get; set; } = 120;


        public int MsgWidth { get; set; } = 780;


        public LogListView()
        {
            rightClickMenu = new ContextMenuStrip();
            ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem
            {
                Text = "清空内容"
            };
            toolStripMenuItem.Click += ClearItems_Click;
            rightClickMenu.Items.Add((ToolStripItem)toolStripMenuItem);
            ContextMenuStrip = rightClickMenu;
            base.MultiSelect = false;
            ColumnHeader value = new ColumnHeader
            {
                Name = "Id",
                Text = "序号",
                TextAlign = HorizontalAlignment.Left,
                Width = IdWidth
            };
            base.Columns.Add(value);
            ColumnHeader value2 = new ColumnHeader
            {
                Name = "Time",
                Text = "时间",
                TextAlign = HorizontalAlignment.Center,
                Width = TimeWidth
            };
            base.Columns.Add(value2);
            ColumnHeader value3 = new ColumnHeader
            {
                Name = "LogType",
                Text = "日志类型",
                TextAlign = HorizontalAlignment.Center,
                Width = LogTypeWidth
            };
            base.Columns.Add(value3);
            headerMsg = new ColumnHeader
            {
                Name = "Msg",
                Text = "日志消息",
                TextAlign = HorizontalAlignment.Left,
                Width = MsgWidth
            };
            base.Columns.Add(headerMsg);
        }

        private void ClearItems_Click(object? sender, EventArgs e)
        {
            base.Items.Clear();
        }

        protected override void OnAutoSizeChanged(EventArgs e)
        {
            base.OnAutoSizeChanged(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
        }

        public void SetLog(string msg, LogLevel logType = LogLevel.Info)
        {
            if (!string.IsNullOrEmpty(msg))
            {
                ClearItems();
                ListViewItem listViewItem = new ListViewItem((base.Items.Count + 1).ToString());
                listViewItem.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                switch (logType)
                {
                    case LogLevel.Debug:
                        listViewItem.SubItems.Add("调试");
                        listViewItem.BackColor = Color.Navy;
                        listViewItem.ForeColor = Color.White;
                        break;
                    case LogLevel.Warning:
                        listViewItem.SubItems.Add("警告");
                        listViewItem.BackColor = Color.Orange;
                        listViewItem.ForeColor = Color.Black;
                        break;
                    case LogLevel.Error:
                        listViewItem.SubItems.Add("错误");
                        listViewItem.BackColor = Color.Red;
                        listViewItem.ForeColor = Color.White;
                        break;
                    default:
                        listViewItem.SubItems.Add("信息");
                        break;
                }

                listViewItem.SubItems.Add(msg);
                base.Items.Insert(0, listViewItem);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.Columns[3].Width = -2;
            base.OnResize(e);
        }
    }
}
